package com.qjq.glasses_shopping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjq.glasses_shopping.pojo.Admin;
import com.qjq.glasses_shopping.result.BaseResult;
import com.qjq.glasses_shopping.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;
    @Resource
    private PasswordEncoder encoder;

    /**
     * 新增管理员
     * @param admin 管理员信息
     * @return 返回结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Admin admin) {
        //对密码加密
        String password = admin.getPassword();
        password= encoder.encode(password);
        admin.setPassword(password);
        adminService.add(admin);
        return BaseResult.ok();
    }

    /**
     * 根据id更新管理员
     * @param admin 管理员信息
     * @return 返回结果
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Admin admin) {
        String password = admin.getPassword();
        if (StringUtils.hasText(password)) {
            password= encoder.encode(password);
            admin.setPassword(password);
        }
        adminService.update(admin);
        return BaseResult.ok();
    }

    /**
     * 删除管理员
     * @param aid 管理员id
     * @return 返回结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long aid) {
        adminService.delete(aid);
        return BaseResult.ok();
    }

    /**
     * 根据id查询管理员全部信息
     * @param aid 管理员id
     * @return 返回结果
     */
    @GetMapping("/findById")
    public BaseResult<Admin> findById(Long aid) {
        Admin admin = adminService.findById(aid);
        return BaseResult.ok(admin);
    }

    /**
     * 分页查询管理员
     * @param page 页码
     * @param size 每页条数
     * @return 返回结果
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/admin/search')")
    public BaseResult<Page<Admin>> search(int page, int size) {
        Page<Admin> adminPage = adminService.search(page, size);
        return BaseResult.ok(adminPage);
    }

    /**
     * 管理员权限管理
     * @param aid 管理员id
     * @param rids 角色id集合
     * @return 执行结果
     */
    @PutMapping("/updateRoleToAdmin")
    public BaseResult updateRoleToAdmin(Long aid, Long[] rids) {
        adminService.updateRoleToAdmin(aid,rids);
        return BaseResult.ok();
    }

    /**
     *  获取登录用户名
     * @return 返回登录的用户名
     */
    @GetMapping("/getUsername")
    public BaseResult getUsername() {
        //获取会话对象
        SecurityContext context = SecurityContextHolder.getContext();
        //获取认证对象
        Authentication authentication = context.getAuthentication();
        //获取用户登录信息
         UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();
        return BaseResult.ok(username);
    }
}
